package com.homegoods.classify.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.classify.presenter.ClassifyListActivityPresent;
import com.homegoods.classify.ui.view.IView;
import com.homegoods.home.R;
import com.homegoods.home.databinding.ActivityClassifyListBinding;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.home.model.HomeClassModel;
import com.homegoods.home.model.HomeGroupsModel;
import com.homegoods.home.model.SubHomeClassModel;
import com.kedll.library.adapter.TabLayoutFragmentPagerAdapter;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.MyUtils;

import java.io.Serializable;
import java.util.List;


/**
 * @author QiQi
 * @descrip 分类详情
 */
@Route(path = ARouterPath.PATH_HOME_ACTIVITY_CLASSIFY)
public class ClassifyListActivityActivity extends MyBaseFragmentActivity<ActivityClassifyListBinding, ClassifyListActivityPresent> implements IView {


    @Autowired(name = "title")
    public String sTitle;
    @Autowired
    public int position;
    @Autowired
    public HomeGroupsModel childlist;

    @Override
    protected int getLayout() {
        return R.layout.activity_classify_list;
    }

    @Override
    protected void initEvent() {
        setTopRightButton(null, R.mipmap.su, v -> ARouter.getInstance()
                .build(ARouterPath.PATH_SEARCH)
                .navigation());
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        ARouter.getInstance().inject(this);
        setTitle(sTitle);
        assert childlist.getChildren() != null;
        mPresenter.onLoadData(childlist.getChildren());
    }

    @Override
    protected ClassifyListActivityPresent getPresenter() {
        return new ClassifyListActivityPresent(this);
    }

    /**
     * 此方法调用方式：当进入此界面，根据当前的分类，请求对应的tabLayout标题
     * 根据请求标题的，来创建对应的Fragment列表
     *
     * @param fragments
     * @param titles
     */
    @Override
    public void initViewPager(List<Fragment> fragments, String[] titles) {
        ViewPager viewPager = findViewById(R.id.viewpager);
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        TabLayoutFragmentPagerAdapter pagerAdapter = new TabLayoutFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        assert viewPager != null;
        viewPager.setAdapter(pagerAdapter);
        assert tabLayout != null;
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(position).select();
    }
}
